package com.me.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.me.entity.PaperQuestion;
import com.me.entity.QuestionLib;
import com.me.entity.StudentPaper;
import com.me.service.ConfigService;
import com.me.service.PaperQuestionService;
import com.me.service.QuestionLibService;
import com.me.service.StudentPaperService;
import com.me.util.EasyUIFormat;
import com.me.util.RandomUtil;

@Controller
@RequestMapping("/questionLib")
@SessionAttributes("questions")
public class QuestionLibController extends BaseController {
	
	/**
	 * 注入业务组件
	 */
	@Resource
	private QuestionLibService questionLibService;
	@Resource
	private StudentPaperService studentPaperService;
	@Resource
	private ConfigService configService;
	@Resource
	private PaperQuestionService paperQuestionService;
	
	/**
	 * 跳转到questionLib主页
	 */
	@RequestMapping("/index.do")
	public String toMember(){
		return "admin/main/question_lib";
	}
	
	/**
	 * 查询所有问答题
	 */
	@RequestMapping("/queryPage.do")
	@ResponseBody
	public Map<String, Object> queryAll(){
		List<QuestionLib> libs = questionLibService.queryAll();
		return EasyUIFormat.formatEntities(libs);
	}
	
	/**
	 * 新增问答题
	 */
	@RequestMapping("/insert.do")
	@ResponseBody
	public Map<String, Object> insert(QuestionLib questionLib){
		try {
			LOG.info("--------插入记录：" + questionLib + "--------");
			questionLibService.insert(questionLib);
			return messageSuccess();
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "插入记录失败！");
		}
	}
	
	/**
	 * 修改问答题
	 */
	@RequestMapping("/update.do")
	@ResponseBody
	public Map<String, Object> update(QuestionLib questionLib){
		try {
			LOG.info("--------修改记录：" + questionLib + "--------");
			questionLibService.update(questionLib);
			return messageSuccess();
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "修改记录失败！");
		}
	}
	
	/**
	 * 转到问答题列表
	 */
	@RequestMapping("/questionList.do")
	public String listQuestion(Model model, HttpSession session){
		// 获取session中的学生记录
		StudentPaper studentPaper = (StudentPaper) session.getAttribute("student");
		LOG.info("-------------Session中的试卷信息：" + studentPaper + "-------------");
/*		// 判断试卷的状态，若试卷状态为1-已经开考, 则不再从题库拉题, 若状态为 3-已经提交试卷，则退出登录
		Integer paperStatus = studentPaperService.getPaperStatus(studentPaper);
		LOG.info("-------------当前试卷状态：" + paperStatus + "-------------");
		if(paperStatus == 3){
			return "redirect:/login/toLogin.do";
		}*/
		
		// 如果session中已经存在问答题，则不再从题库拉取
		if(session.getAttribute("questions") != null){
			return "student/main/question_list";
		}
		// 开始拉取题库中的问答题
		try {
			// 获取整个题库的问答题
			List<QuestionLib> questionsAll= questionLibService.queryAll();
			// 题库中的总问答题数量
			int totalSize = questionsAll.size();
			// 系统设置好的选择题个数
			int configSize = configService.getQuestionNum();
			// 封装最终的问答题
			List<QuestionLib> questions = new ArrayList<>();
			if(totalSize >= configSize){
				// 生成随机下标
				int[] nums = RandomUtil.genRandomNums(totalSize, configSize);
				// 随机筛选选择题
				for(int index : nums){
					questions.add(questionsAll.get(index));
				}
			} else{
				questions.addAll(questionsAll);
			}
			
			// 将这些问答题的信息更新到试卷问答题表中
			// 输入参数：试卷编号， 问答题题号
			Integer paperId = studentPaper.getId();
			// 1.查找该试卷绑定的所有问答题
			List<PaperQuestion> oldList = paperQuestionService.queryByPaperId(paperId);
			if(oldList != null){
				for(PaperQuestion paperQuestion : oldList){
					// 2.删除绑定记录
					paperQuestionService.delete(paperQuestion.getId());
				}
			}
			// 3.新增绑定关系
			for(QuestionLib question : questions){
				paperQuestionService.insertBind(paperId, question.getId());
			}
			model.addAttribute("questions", questions);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "student/main/question_list";
	}
}
